<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{		
		$this->load->view('login');		
	}
	
	public function doLogin(){
		$this->load->model ( 'master/users', 'user' );		
		
		if(isset($_POST['username']) && isset($_POST['password'])){
		
			$username = $_POST['username'];
			$password = $_POST['password'];
		}else{
			show_error('Login or Password are incorrect. Please try <a href="'.base_url().'">login</a> again.',"500","Access Denied");			
		}
		
		if($this->user->authenUser($username,$password)){
			
			// Gen sessionid
			$session_id = md5 ($username . $password);
				
			$user_data = array (	
					'loginname' => $username,				
					'session_id' => $session_id,
					'is_logged_in' => true
			);
			
			$this->session->set_userdata ($user_data);
			$data["loginname"] = $username;			
			$this->load->view('template/header');
			$this->load->view('main',$data);
			$this->load->view('template/main_footer');
					
		}else{			
			//$this->session->set_userdata ($session_id);
			$this->session->sess_destroy ();
			
			show_error('Login or Password are incorrect. Please try <a href="'.base_url().'">login</a> again.',"500","Access Denied");			
		}
		
	}
	
	public function doLogout(){
		$this->session->sess_destroy();
		$this->load->view('login');
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */